package net.wanho.ums.controller;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.ums.entity.User;
import net.wanho.ums.service.UserService;
import net.wanho.ums.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-08-15 16:39
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user){
        userService.add(user);
        return AjaxResult.success();
    }

    /**
     * 根据id删除用户
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Integer id){
        userService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/removeBatch")
    public AjaxResult removeBatch(@RequestBody List<Integer> ids){
        userService.removeBatch(ids);
        return AjaxResult.success();
    }

    /**
     * 修改用户
     */
    @PutMapping("/modify")
    public AjaxResult modify(@RequestBody User user){
        userService.modify(user);
        return AjaxResult.success();
    }

    /**
     * 修改用户状态
     */
    @PutMapping("/modifyStatus/{id}/{status}")
    public AjaxResult modifyStatus(@PathVariable Integer id, @PathVariable Integer status){
        userService.modifyStatus(id, status);
        return AjaxResult.success();
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/list")
    public AjaxResult list(){
        List<User> list = userService.list();
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询用户
     */
    @GetMapping("/query/{id}")
    public AjaxResult query(@PathVariable Integer id){
        User user = userService.queryById(id);
        return AjaxResult.success(user);
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/queryByPage")
    public AjaxResult queryByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize, User user){
        PageInfo<User> pageInfo = userService.queryByPage(pageNum, pageSize, user);
        return AjaxResult.success(pageInfo);
    }
}
